﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using BUS;
using TuyenDungViecLam.CommonResource;

namespace TuyenDungViecLam.NguoiTimViec
{
    public partial class UploadCV : BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int Mct = int.Parse(Request.QueryString["Mct"].ToString());
                //int Mct = 2;

                LbNhaTuyenDung.Text = NhaTuyenDung_BUS.LayTenNhaTuyenDung(Mct, "");
                ViewState["UploadThanhCong"] = Convert.ToString(0);
            }
        }

        protected void BtUpload_Click(object sender, EventArgs e)
        {
            
            if (FileUploadCV.HasFile)  //if file uploaded
            {
                int userID = int.Parse(Session["UserID"].ToString());
                //int userID = 2;

                if (checkFileType(FileUploadCV.FileName))  //Check for file types
                {
                    try
                    {

                        //kiem tra đóng phí
                        int kq = NguoiTimViec_BUS.KiemTraDongPhiCuaNguoiTimViec(userID);
                        if (kq == 1)
                        {
                            //kiem tra ten file nay ton tai chua?
                            string fileName = "";
                            FileInfo f = new FileInfo(MapPath("~/Upload/" + FileUploadCV.FileName));
                            if (f.Exists)
                            {
                                int vt = f.Name.IndexOf('.');
                                string name = f.Name.Substring(0, vt);
                                fileName = name + "_UserID" + userID.ToString() + f.Extension;
                            }
                            else
                            {
                                fileName = FileUploadCV.FileName;
                            }

                            //save file
                            lblMsg.Text = "đang tải lên server...";
                            FileUploadCV.SaveAs(MapPath("~/Upload/" + fileName));

                            lblMsg.Text = FileUploadCV.FileName + " Upload thành công!";
                            ViewState["filename"] = fileName;
                            // bien kiem tra co update thanh cong hay chua
                            ViewState["UploadThanhCong"] = Convert.ToString(1);
                        }
                        else
                            if (kq == 0)
                                Alert.Show("Vui lòng đóng phí để được upload CV");
                    }
                    catch (System.IO.DirectoryNotFoundException)
                    {
                        createDir(userID);
                    }
                }
                else
                {
                    Alert.Show("Kiểu file không đúng. vui lòng upload file : .doc , .docx, pdf, txt");
                }
            }
            //else
            //{
            //    Response.Write("<script language =Javascript> alert('Invalid File Format, choose .gif,.png..jpg.jpeg');</script>");
            //}
        }

        private bool checkFileType(string fileName)
        {
            string fileExt = Path.GetExtension(FileUploadCV.FileName);

            switch (fileExt.ToLower())
            {
                case ".doc":
                    return true;
                case ".docx":
                    return true;
                case ".pdf":
                    return true;
                case ".txt":
                    return true;
                default:
                    return false;
            }

        }

        private void createDir(int userID)
        {
            DirectoryInfo myDir = new DirectoryInfo(MapPath("~/Upload/"));
            myDir.Create();

            //Now save file
            int kq = NguoiTimViec_BUS.KiemTraDongPhiCuaNguoiTimViec(userID);
            if (kq == 1)
            {
                //kiem tra ten file nay ton tai chua?
                string fileName = "";
                FileInfo f = new FileInfo(MapPath("~/Upload/" + FileUploadCV.FileName));
                if (f.Exists)
                {
                    int vt = f.Name.IndexOf('.');
                    string name = f.Name.Substring(0, vt);
                    fileName = f.Name + "_UserID" + userID.ToString()+ f.Extension;
                }
                else
                {
                    fileName = FileUploadCV.FileName;
                }

                //save file
                lblMsg.Text = "đang tải lên server...";
                FileUploadCV.SaveAs(MapPath("~/Upload/" + fileName));

                lblMsg.Text = FileUploadCV.FileName + " Upload thành công!";
                ViewState["filename"] = fileName;
                // bien kiem tra co update thanh cong hay chua
                ViewState["UploadThanhCong"] = Convert.ToString(1);
            }
            else
                if (kq == 0)
                    Alert.Show("Vui lòng đóng phí để được upload CV");
        }

        protected void BtGui_Click(object sender, EventArgs e)
        {
            string kiemTraUploadFile = ViewState["UploadThanhCong"].ToString();
            if ( kiemTraUploadFile == "1")
            {
                int Mct = int.Parse(Request.QueryString["Mct"].ToString());
                //int Mct = 2;

                int userID = int.Parse(Session["UserID"].ToString());
                //int userID = 2;

                string fileName = ViewState["filename"].ToString();
                bool kq = NguoiTimViec_BUS.GuiCVUploadChoNhaTuyenDung(userID, Mct, fileName, "~/Upload/" + fileName);

                if (kq == true)
                    Alert.ShowAndRedirect("Gửi thành công", "../../Default.aspx");
                else
                    Alert.Show("Gửi thật bại");
            }
            else
            {
                Alert.Show("Bạn chưa update file");
            }
        }
       
    }
}
